//
//  PhotoManageCell.m
//  KuWoCan
//
//  Created by hwd on 17/3/16.
//  Copyright © 2017年 hwd. All rights reserved.
//

#import "PhotoManageCell.h"
#import "PicManageVC.h"
#import "PicListModel.h"
#import "UIButton+WebCache.h"
#import "UploadPicVC.h"
#import "PhotoManageCollectionViewCell.h"
#import "PicPageListModel.h"
static NSString *const CollectionIdentifier = @"CollectionCell";

@interface PhotoManageCell ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView *collectionV;
@property (nonatomic, strong) NSMutableArray<PicListModel *> *dataArr;
@property (nonatomic, strong) NSMutableArray<PicListModel *> *picArr;
@end

@implementation PhotoManageCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self configUI];
        [self setGuestureRecognizer];
        self.backgroundColor = [UIColor whiteColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

- (void)configUI {
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    self.titleLabel.font = KwFont(15 * screenW_6);
    self.titleLabel.backgroundColor = [UIColor clearColor];
    self.titleLabel.textColor = [UIColor grayColor];
    [self.contentView addSubview:self.titleLabel];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@20);
        make.right.equalTo(self.contentView);
        make.top.equalTo(self.contentView);
        make.height.equalTo(@(55 * screenW_6));
    }];

    UIView *lineView = [[UIView alloc] initWithFrame:CGRectZero];
    lineView.backgroundColor = KwGaryColor;
    [self.contentView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(1));
        make.right.equalTo(self.contentView);
        make.left.equalTo(self.contentView);
        make.top.equalTo(self.titleLabel.mas_bottom);
    }];

    UICollectionViewFlowLayout *flowLayout = [UICollectionViewFlowLayout new];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.minimumLineSpacing = 5 *screenW_6;
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.itemSize = CGSizeMake(118 * screenW_6, 118 * screenW_6);
    self.collectionV = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
    self.collectionV.backgroundColor = [UIColor whiteColor];
    self.collectionV.showsVerticalScrollIndicator = NO;
    self.collectionV.showsHorizontalScrollIndicator = NO;
    [self.collectionV registerClass:[PhotoManageCollectionViewCell class] forCellWithReuseIdentifier:CollectionIdentifier];
    self.collectionV.delegate = self;
    self.collectionV.dataSource = self;
    [self.contentView addSubview:self.collectionV];
    [self.collectionV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(20 * screenW_6));
        make.top.equalTo(lineView.mas_bottom).offset(20 * screenW_6);
        make.right.equalTo(self.contentView);
        make.height.equalTo(@(118 * screenW_6));
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-20 * screenW_6);
    }];
    UIImageView *arrowImageV = [[UIImageView alloc] initWithImage:kImg(@"next")];
    [self.contentView addSubview:arrowImageV];
    [arrowImageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.equalTo(@(25 * screenW_6));
        make.size.mas_equalTo(CGSizeMake(25 * screenW_6, 25 * screenW_6));
        make.centerY.equalTo(self.titleLabel.mas_centerY);
        make.right.equalTo(@(-20));
    }];
}

- (void)setGuestureRecognizer {
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(turnToPicManage)];
    self.titleLabel.userInteractionEnabled = YES;
    [self.titleLabel addGestureRecognizer:tap];
}

- (void)configCell:(NSArray<PicListModel *> *)arr {
    self.dataArr = arr.mutableCopy;
    [self.collectionV reloadData];
}

#pragma mark - UICollectionViewDeleaget
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArr.count > 0 ? self.dataArr.count : 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    PhotoManageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CollectionIdentifier forIndexPath:indexPath];
    if (self.dataArr.count > 0) {
        [cell setImageWithPicListModel:self.dataArr[indexPath.row]];
//        cell.backgroundImageV.hidden = YES;
    } else {
        [cell setImageWithPicListModel:nil];
//        cell.backgroundImageV.hidden = NO;
    }
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [self turnToPicManage]; 
}
#pragma mark - Other
- (void)turnToPicManage
{
    if (_isChange || _isSubmit)
    {
        UploadPicVC *vc = [[UploadPicVC alloc] init];
        vc.orderId = _orderId;
        vc.isSubmit = self.isSubmit;
        vc.picType = _picType;
        vc.isChange = _isChange;
        vc.picMutableArr = self.dataArr;
        vc.picParaArr = self.dataArr;
        vc.describStr = @"";
        __weak typeof(self) weakSelf = self;
        vc.chooseComplete = ^(NSMutableArray *picData) {
            //移除且加入已经是model的类型
            _picArr = [NSMutableArray new];
            for (int i = (int)(picData.count-1); i>=0; i--) {
                if ([picData[i] isKindOfClass:[PicListModel class]]) {
                    [self.picArr addObject:picData[i]];
                    [picData removeObjectAtIndex:i];
                }
            }
            //转化为model加入
            [picData enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                PicListModel *model = [PicListModel new];
                model.imgs = obj[@"imgs"];
                model.title = obj[@"title"];
                [self.picArr addObject:model];
            }];
            
            [weakSelf.dataArr removeAllObjects];
            weakSelf.dataArr = _picArr.mutableCopy;
            if (weakSelf.choosePictureHandle) {
                weakSelf.choosePictureHandle(_picArr);
            }
            [weakSelf.collectionV reloadData];
        };
        [self.vc.navigationController pushViewController:vc animated:YES];
    }
    else
    {
        PicManageVC *picVC = [[PicManageVC alloc] init];
        picVC.picType = _picType;
        picVC.orderId = _isChange ? _orderId : @"";
        picVC.isChange = _isChange;
        picVC.isSubmit = _isSubmit;
        picVC.picArr = self.dataArr;
        __weak typeof(self) weakSelf = self;
        picVC.pictureCallback = ^(NSArray<PicListModel *> *picArr) {
            [weakSelf.dataArr removeAllObjects];
            weakSelf.dataArr = picArr.mutableCopy;
            if (weakSelf.choosePictureHandle) {
                weakSelf.choosePictureHandle(picArr);
            }
            [weakSelf.collectionV reloadData];
        };
        [[UIApplication sharedApplication].keyWindow endEditing:YES];
        [self.vc.navigationController pushViewController:picVC animated:YES];
    }
}


#pragma mark - getter 
-(NSMutableArray<PicListModel *> *)dataArr {
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}


@end
